<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader
{
	/**
	 * List of loaded sercices
	 *
	 * @var array
	 * @access protected
	 */
	protected $_ci_services = array();
	/**
	 * List of paths to load sercices from
	 *
	 * @var array
	 * @access protected
	 */
	protected $_ci_service_paths		= array();

	/**
	 * Constructor
	 * 
	 * Set the path to the Service files
	 */
	public function __construct()
	{
		parent::__construct();
        $this->CI = &get_instance();
		$this->_ci_service_paths = array(APPPATH);
	}

	public function add_package_path($path, $view_cascade=TRUE)
	{
		parent::add_package_path($path, $view_cascade);

		array_unshift($this->_ci_service_paths, $path);
	}

	/**
	 * Service Loader
	 * 
	 * This function lets users load and instantiate classes.
	 * It is designed to be called from a user's app controllers.
	 *
	 * @param	string	the name of the class
	 * @param	mixed	the optional parameters
	 * @param	string	an optional object name
	 * @return	void
	 */
	public function service($service = '', $params = NULL, $object_name = NULL)
	{
		if(is_array($service))
		{
			foreach($service as $class)
			{
				$this->service($class, $params);
			}
			return;
		}

		if($service == '' or isset($this->_ci_services[$service])) {
			return FALSE;
		}

		if(! is_null($params) && ! is_array($params)) {
			$params = NULL;
		}

		$subdir = '';

		// Is the service in a sub-folder? If so, parse out the filename and path.
		if (($last_slash = strrpos($service, '/')) !== FALSE)
		{
				// The path is in front of the last slash
				$subdir = substr($service, 0, $last_slash + 1);

				// And the service name behind it
				$service = substr($service, $last_slash + 1);
		}

		foreach($this->_ci_service_paths as $path)
		{

			$filepath = $path .'services/'.$subdir.$service.'.php';

			if ( ! file_exists($filepath))
			{
				continue;
			}

			include_once($filepath);

			$service = strtolower($service);

			if (empty($object_name))
			{
				$object_name = $service;
			}

			//$service = ucfirst($service);
			$CI = &get_instance();
			if($params !== NULL)
			{
				$CI->$object_name = new $service($params);
			}
			else
			{
				$CI->$object_name = new $service();
			}

			$this->_ci_services[] = $object_name;

			return;
		}
	}

    public function view($view, $vars = array(), $return = FALSE){
        $cur_nav = $this->CI->uri->segment(1, 0);//取得当前访问的频道
        if (!in_array("{$cur_nav}_", array('admin_'))) {
            $navs = array('model','artist','act','ins');//定义所有的导航频道
            foreach ($navs as $v){
                if ($cur_nav == $v) {//为当前访问的频道设定值
                    $vars["{$v}_arrParam"] = isset($vars['arrParam'])?$vars['arrParam']:array();
                } else {
                    $vars["{$v}_arrParam"] = array();
                }
            }
            $vars['nav_seled'] = '';
            //当访问的是模特页时, 对访问的菜单值拼接 并 对参数做处理
            if ('model_'=="{$cur_nav}_") {
                $nav_param = $vars["{$cur_nav}_arrParam"];
                if (isset($nav_param['label'])) {//标签 和 区域 参数不能并存
                    if (isset($nav_param['area'])) unset($nav_param['area']);
                    $model_navs = array($nav_param['sex'],$nav_param['label']);
                } elseif (isset($nav_param['area'])) {
                    if (isset($nav_param['label'])) unset($nav_param['label']);
                    $model_navs = array($nav_param['sex'],$nav_param['area']);
                } else {
                    $model_navs = $nav_param['sex'];
                }
                $vars['nav_seled'] = is_array($model_navs)?implode('-',$model_navs):$model_navs;
            }
            //取得模特的区域类型
            $vars['nav_areaType'] = _get_config('area_type',true);
            //取得全部模特标签(中文内容)
            $this->CI->load->model('User_model');
            $this->CI->load->model('user/Model_model');
            $vars['model_label'] = $this->CI->Model_model->get_label('',true);
            $insType = _get_config('type', true);
            //取得艺人类型
            $vars['nav_artist'] = $insType[6];
            //取得公司类型
            $vars['nav_ins'] = $insType[2];
            //取得通告类型
            $vars['nav_act'] = _get_config('activity', true);
        }
        parent::view($view, $vars, $return);
    }

}
